﻿using System.Windows.Input;
using FileTransferDesktop.ViewModels.Controls;
using FileTransferDesktop.ViewModels.Windows;
using Microsoft.Extensions.DependencyInjection;
using Wpf.Ui;

namespace FileTransferDesktop.Controls
{
    public partial class UserMenuControl : UserControl
    {
        public UserMenuControlViewModel ViewModel => (UserMenuControlViewModel)DataContext;

        public UserMenuControl()
        {
            InitializeComponent();

            // 设置 DataContext 为 ViewModel
            if (App.Services != null)
            {
                DataContext = App.Services.GetService<UserMenuControlViewModel>();
            }
        }

        private void LogoutTextBlock_Click(object sender, MouseButtonEventArgs e)
        {
            // 使用 ViewModel 的登出命令
            if (ViewModel?.LogoutCommand?.CanExecute(null) == true)
            {
                ViewModel.LogoutCommand.Execute(null);
            }
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            // 调用主窗口的登录功能
            if (
                App.Services?.GetService<MainWindowViewModel>() is MainWindowViewModel mainViewModel
            )
            {
                if (mainViewModel.ShowLoginDialogCommand?.CanExecute(null) == true)
                {
                    mainViewModel.ShowLoginDialogCommand.Execute(null);
                }
            }
        }

        private void RegisterButton_Click(object sender, RoutedEventArgs e)
        {
            // 调用主窗口的注册功能
            if (
                App.Services?.GetService<MainWindowViewModel>() is MainWindowViewModel mainViewModel
            )
            {
                if (mainViewModel.ShowRegisterDialogCommand?.CanExecute(null) == true)
                {
                    mainViewModel.ShowRegisterDialogCommand.Execute(null);
                }
            }
        }

        // 保留原有的导航方法作为备用（虽然现在主要使用 ViewModel 的命令）
        private void UserProfileButton_Click(object sender, RoutedEventArgs e)
        {
            // 获取主窗口并导航到用户资料页
            if (Application.Current.MainWindow is INavigationWindow navWindow)
            {
                navWindow.Navigate(typeof(FileTransferDesktop.Views.Pages.UserProfilePage));
            }
        }

        private void ManageButton_Click(object sender, RoutedEventArgs e)
        {
            // 获取主窗口并导航到管理页
            if (Application.Current.MainWindow is INavigationWindow navWindow)
            {
                navWindow.Navigate(typeof(FileTransferDesktop.Views.Pages.ManagementPage));
            }
        }

        private void SettingsButton_Click(object sender, RoutedEventArgs e)
        {
            // 获取主窗口并导航到设置页
            if (Application.Current.MainWindow is INavigationWindow navWindow)
            {
                navWindow.Navigate(typeof(FileTransferDesktop.Views.Pages.SettingsPage));
            }
        }
    }
}
